package com.buyhouse.zhaimao;

import java.util.LinkedList;
import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {
	
	public static Context context;
	private static MyApplication application;
	public String userAgent;
	/**activity集合**/
	private List<Activity> activities = new LinkedList<Activity>();
	public DisplayImageOptions options;
	@Override
	public void onCreate() {		
		super.onCreate();	
		application = this;
		context = getApplicationContext();				
	}
	public MyApplication() {
		super();
	}
	
	public static MyApplication getInstance() {
		if (application == null)
			application = new MyApplication();
		return application;
	}
	/**
	 * 保存activity到现有列表中
	 * 
	 * @param activity
	 */
	public void addActivity(Activity activity) {
		activities.add(activity);
	}

	public void removeActiviyt(Activity activity) {
		activities.remove(activity);
	}

	/**
	 * 退出所有activity
	 */
	public void exit() {
		try {
			if (activities != null) {
				// for(Activity a : activities){
				// a.finish();
				// }
				Activity a;
				int aCount = activities.size();
				for (int i = 0; i < aCount; i++) {
					a = activities.get(i);
					if (a != null) {
						if (!a.isFinishing()) {
							a.finish();
						}
						a = null;
					}
				}
				activities.clear();
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			//System.exit(0);
		}
	}
	
}
